//
//  UserAccount.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/11.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {

    var access_token: String?
    var expires_in: NSTimeInterval = 0 {
        
        didSet{
        
            let nowDate = NSDate()
            
            expiresDate = nowDate.dateByAddingTimeInterval(expires_in)
        }
    }
    
    var expiresDate : NSDate?
    var remind_in: String?
    var uid: String?    //当前授权用户的UID。
    var name: String?  //用户友好显示名称
    var avatar_large: String?     //用户头像地址（大图），180×180像素

    
    static let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.archive")
    
    init(dict: [String : AnyObject]){
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    // 加载用户信息,负责存储数据
    func loadUserInfo(completion:(error: NSError?)->()){
        
        NetworkTools.shatedToos.loadUserInfo(access_token!, uid: uid!) { (dict, error) -> () in
            if error != nil {
                completion(error: error)
                return
            }
            
            self.name = dict!["name"] as? String
            self.avatar_large = dict!["avatar_large"] as? String
            self.saceAccount()
            
            completion(error: nil)
        }
        
    }
    
    
    // 保存当前账户到本地
    func saceAccount() {
        
       NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.path)
        printLog(UserAccount.path)
    }
    
    class func loadUserAccount() -> UserAccount? {
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(UserAccount.path) as? UserAccount {
            
            if account.expiresDate?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                return account
            }
        }
        return nil
    }
    
    
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    
    required init?(coder aDecoder: NSCoder){
    
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
    }
    
   override var description: String {
        
        let keys = ["access_token", "expires_in", "remind_in", "uid", "name", "avatar_large"]
    
        return dictionaryWithValuesForKeys(keys).description
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
}
